package ah.cgen;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ah.file.FileCopy;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * 使用已存在的对象，替换掉可变的部分，作为模板
 * @author Administrator
 *
 */
public class Gen {
	private final static String BASE_PACKAGE = "com.wos.mpbao";
	private Map<String, Object> root;
	private Configuration cfg;
	
	public Gen() throws IOException {
		root = new HashMap<String, Object>();
		root.put("basePackage", BASE_PACKAGE);
		
		cfg = new Configuration();
		cfg.setDirectoryForTemplateLoading(new File("src/test/mpb/config"));//模板目录
		cfg.setDefaultEncoding("UTF-8");
	}
	
	/**
	 * 模板,生成对象
	 * @param beanName
	 * @throws Exception
	 */
	public void genJava(String beanName) throws Exception {
		genDAO(beanName);
		genService(beanName);
		genAction(beanName);
	}
	
	/**
	 * spring xml
	 * @param beanNames
	 * @throws IOException
	 * @throws TemplateException
	 */
	public void genSpringXml(List<String> beanNames) throws IOException, TemplateException {
		Template t = cfg.getTemplate("config/applicationContext.ftl");
		
		File dir = new File("tmp/config");
		if (!dir.exists()) {
			dir.mkdirs();
		}
		
		root.put("beans", beanNames);
		
		FileOutputStream fos = new FileOutputStream("tmp/config/applicationContext.xml");
		OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
		BufferedWriter bw = new BufferedWriter(osw);
		t.process(root, bw);
		osw.close();
		fos.close();
	}
	
	/**
	 * DAO DAOImpl
	 * @param beanName
	 * @throws Exception
	 * @author ddh
	 */
	public void genDAO(String beanName) throws Exception {
		String content = FileCopy.readFileStr("src/test/mpb/com/wos/mpbao/database/dao/KhhTestDAO.java");
		content = content.replaceAll("KhhTest", beanName);
		content = content.replaceAll("khhTest", beanName.substring(0, 1).toLowerCase() + beanName.substring(1));
		
		//接口
		
		String servicePackagePath = (BASE_PACKAGE + ".database.dao").replace(".", "/");
		File dir = new File("tmp/" + servicePackagePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		
		FileOutputStream fos = new FileOutputStream("tmp/"+ servicePackagePath +"/"+ beanName +"DAO.java");
		OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
		BufferedWriter bw = new BufferedWriter(osw);
		bw.write(content);
		bw.flush();
		osw.close();
		fos.close();
		
		//实现
		content = FileCopy.readFileStr("src/test/mpb/com/wos/mpbao/database/dao/impl/KhhTestDAOImpl.java");
		content = content.replaceAll("KhhTest", beanName);
		content = content.replaceAll("khhTest", beanName.substring(0, 1).toLowerCase() + beanName.substring(1));
		String serviceImplPackagePath = (BASE_PACKAGE + ".database.dao.impl").replace(".", "/");
		dir = new File("tmp/" + serviceImplPackagePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		
		fos = new FileOutputStream("tmp/"+ serviceImplPackagePath +"/"+ beanName +"DAOImpl.java");
		osw = new OutputStreamWriter(fos, "UTF-8");
		bw = new BufferedWriter(osw);
		bw.write(content);
		bw.flush();
		osw.close();
		fos.close();
	}
	
	/**
	 * Service
	 * @param beanName
	 * @throws Exception
	 * @author ddh
	 */
	public void genService(String beanName) throws Exception {
		String content = FileCopy.readFileStr("src/test/mpb/com/wos/mpbao/service/KhhTestService.java");
		content = content.replaceAll("KhhTest", beanName);
		content = content.replaceAll("khhTest", beanName.substring(0, 1).toLowerCase() + beanName.substring(1));
		
		//接口
		
		String servicePackagePath = (BASE_PACKAGE + ".service").replace(".", "/");
		File dir = new File("tmp/" + servicePackagePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		
		FileOutputStream fos = new FileOutputStream("tmp/"+ servicePackagePath +"/"+ beanName +"Service.java");
		OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
		BufferedWriter bw = new BufferedWriter(osw);
		bw.write(content);
		bw.flush();
		osw.close();
		fos.close();
		
		//实现
		content = FileCopy.readFileStr("src/test/mpb/com/wos/mpbao/service/impl/KhhTestServiceImpl.java");
		content = content.replaceAll("KhhTest", beanName);
		content = content.replaceAll("khhTest", beanName.substring(0, 1).toLowerCase() + beanName.substring(1));
		String serviceImplPackagePath = (BASE_PACKAGE + ".service.impl").replace(".", "/");
		dir = new File("tmp/" + serviceImplPackagePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		
		fos = new FileOutputStream("tmp/"+ serviceImplPackagePath +"/"+ beanName +"ServiceImpl.java");
		osw = new OutputStreamWriter(fos, "UTF-8");
		bw = new BufferedWriter(osw);
		bw.write(content);
		bw.flush();
		osw.close();
		fos.close();
	}
	
	/**
	 * Action
	 * @param beanName
	 * @throws Exception
	 * @author ddh
	 */
	public void genAction(String beanName) throws Exception {
		String content = FileCopy.readFileStr("src/test/mpb/com/wos/mpbao/action/mgr/KhhTestAction.java");
		content = content.replaceAll("KhhTest", beanName);
		content = content.replaceAll("khhTest", beanName.substring(0, 1).toLowerCase() + beanName.substring(1));
		
		String actionPackagePath = (BASE_PACKAGE + ".action.mgr").replace(".", "/");
		File dir = new File("tmp/" + actionPackagePath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		
		FileOutputStream fos = new FileOutputStream("tmp/"+ actionPackagePath +"/"+ beanName +"Action.java");
		OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
		BufferedWriter bw = new BufferedWriter(osw);
		bw.write(content);
		bw.flush();
		osw.close();
		fos.close();
	}
}
